<?php
class BannerController extends Controller{
    public $layout='//layouts/admin';
    public function Upload($file){
        $fileupload = $file['file_upload_banner'];
        $tmp=$fileupload['tmp_name'];
        $filename=time().$fileupload['name'];
        $destinal='uploads/banner/'.$filename;
        move_uploaded_file($tmp,$destinal);
        return $destinal;
    }

    public function MultipleUpload($slide,$id_product){
        foreach($slide['name'] as $key =>$value){
            $fileupload = $slide;
            $tmp=$fileupload['tmp_name'][$key];
            $filename=time().$fileupload['name'][$key];
            $destinal='uploads/banner/'.$filename;
            move_uploaded_file($tmp,$destinal);
            $modelimages = new Images();
            $modelimages ->destination = $destinal;
            $modelimages ->product_id = $id_product;
            $modelimages->save();
        }
    }

    public function actionindex(){
        $this->render('index');
    }
    public function actionPreInsertBanner(){
        session_start();
        $model = new Typebanner();
        $listtypebanner = $model->findAll();
        $this->render('insertbanner',array('listtypebanner'=>$listtypebanner));
    }
    public function actionInsertBanner(){
        session_start();
        $images = $this->Upload($_FILES);
        $model = new Banner();
        $model->destination = $images;
        $model->banner_name = $_POST['banner_name'];
        $model->admin_id = $_SESSION['admin']['id'];
        $model->type_id = $_POST['type_id'];
        $model->save();
        Yii::app()->user->setFlash('contact','thêm mới banner thành công');
        $this->redirect('index.php?r=admin/banner/listbanner');
    }
    public function actionPreEditBanner(){

    }
    public function actionListBanner(){
        $model = new Banner();
        $bannerleft = $model ->findAll("type_id=1");
        $bannerright = $model ->findAll("type_id=2");
        $this->render('listbanner',array('bannerleft'=>$bannerleft,'bannerright'=>$bannerright));
    }

    public function actionDeleteBanner(){
        if(is_numeric($_GET['id'])==true){$id = $_GET['id'];}else{$id = 0;}
        echo $id;
        $images = $model = Banner::model()->FindByPk($id);
        unlink($images->destination);
        $model->delete();
        Yii::app()->user->setFlash('contact','xóa banner thành công');
        $this->redirect('index.php?r=admin/banner/listbanner');
    }
}